﻿Imports System.Configuration
Imports System.Windows

Namespace Coding4Fun.PrinterDisplayHack
	''' <summary>
	''' Interaction logic for App.xaml
	''' </summary>
	Partial Public Class App
		Inherits Application

		Private _notifyIcon As System.Windows.Forms.NotifyIcon
		Private _mainWindow As MainWindow

		Private Sub App_DispatcherUnhandledException(ByVal sender As Object, ByVal e As System.Windows.Threading.DispatcherUnhandledExceptionEventArgs)
			' stop the timer
			_mainWindow.StopPrinterHacking()

			' display the error
			_mainWindow.LogText("Sending the stock prince to the printer was halted due to this error:" & e.Exception.ToString())

			' display the form
			ShowMainForm()

			' Log the error to a file and notify the user
			Dim theException As Exception = e.Exception
			Dim theErrorPath As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & "\PrinterDisplayHackError.txt"
			Using theTextWriter As System.IO.TextWriter = New System.IO.StreamWriter(theErrorPath, True)
				Dim theNow As Date = Date.Now
				theTextWriter.WriteLine(String.Format("The error time: {0} {1}", theNow.ToShortDateString(), theNow.ToShortTimeString()))
				Do While theException IsNot Nothing
					theTextWriter.WriteLine("Exception: " & theException.ToString())
					theException = theException.InnerException
				Loop
			End Using
			MessageBox.Show("An unexpected error occurred.  A stack trace can be found at:" & vbLf & theErrorPath)
			e.Handled = True
		End Sub

		Private Sub ShowMainForm()
			' show the window in the normal state
			_mainWindow.Show()
			_mainWindow.WindowState = WindowState.Normal
		End Sub
		Private Sub notifyIcon_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
			ShowMainForm()
		End Sub

		Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
			_notifyIcon = New System.Windows.Forms.NotifyIcon()
			AddHandler _notifyIcon.DoubleClick, AddressOf notifyIcon_DoubleClick

			_notifyIcon.Icon = My.Resources.Icon
			_notifyIcon.Visible = True

			Dim items() As System.Windows.Forms.MenuItem = {New System.Windows.Forms.MenuItem("&Settings", AddressOf Settings_Click) With {.DefaultItem = True}, New System.Windows.Forms.MenuItem("-"), New System.Windows.Forms.MenuItem("&Exit", AddressOf Exit_Click)}

			_notifyIcon.ContextMenu = New System.Windows.Forms.ContextMenu(items)

			_mainWindow = New MainWindow()

			If Not _mainWindow.SettingsAreValid() Then
				_mainWindow.Show()
			Else
				_mainWindow.StartPrinterHacking()
			End If
		End Sub

		Private Sub Settings_Click(ByVal sender As Object, ByVal e As EventArgs)
			_mainWindow.Show()
		End Sub

		Private Sub Exit_Click(ByVal sender As Object, ByVal e As EventArgs)
			Application.Current.Shutdown()
		End Sub

		Private Sub Application_Exit(ByVal sender As Object, ByVal e As ExitEventArgs)
			_notifyIcon.Dispose()
		End Sub
	End Class
End Namespace
